﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Design;
using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Drawing;

namespace MediaCenter.Controls.Design
{
    public class ElboTypeEditor : UITypeEditor
    {
        private ElboUI elboUI;
        public ElboTypeEditor()
        {

        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (provider != null)
            {
                IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if (edSvc == null)
                {
                    return value;
                }
                if (this.elboUI == null)
                {
                    this.elboUI = new ElboUI();
                }
                this.elboUI.Start(edSvc, value);
                edSvc.DropDownControl(this.elboUI);
                value = this.elboUI.Value;
                this.elboUI.End();
            }
            return value;

        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        private class ElboUI : Control
        {
            private Button obenlinks;
            private Button obenrechts;
            private Button untenlinks;
            private Button untenrechts;
            private IWindowsFormsEditorService edSvc;
            private object value;

            public ElboUI()
            {
                InitComponent();
            }

            private void InitComponent()
            {
                base.Size = new Size(0x7d, 0x59);
                this.BackColor = SystemColors.Control;
                this.ForeColor = SystemColors.ControlText;

                this.obenlinks = new Button();
                this.obenlinks.Size = new Size(0x3E, 0x2C);
                this.obenlinks.Location = new Point(0, 0);
                this.obenlinks.Text = "";
                this.obenlinks.Name = "ol";

                this.obenrechts = new Button();
                this.obenrechts.Size = new Size(0x3E, 0x2C);
                this.obenrechts.Location = new Point(0x3E, 0);
                this.obenrechts.Text = "";
                this.obenlinks.Name = "or";

                this.untenlinks = new Button();
                this.untenlinks.Size = new Size(0x3E, 0x2C);
                this.untenlinks.Location = new Point(0, 0x2C);
                this.untenlinks.Text = "";
                this.obenlinks.Name = "ul";

                this.untenrechts = new Button();
                this.untenrechts.Size = new Size(0x3E, 0x2C);
                this.untenrechts.Location = new Point(0x3E, 0x2C);
                this.untenrechts.Text = "";
                this.obenlinks.Name = "ur";

                this.obenlinks.Click += new EventHandler(this.OptionClick);
                this.obenrechts.Click += new EventHandler(this.OptionClick);
                this.untenlinks.Click += new EventHandler(this.OptionClick);
                this.untenrechts.Click += new EventHandler(this.OptionClick);

                this.Controls.Clear();
                this.Controls.Add(this.obenlinks);
                this.Controls.Add(this.obenrechts);
                this.Controls.Add(this.untenlinks);
                this.Controls.Add(this.untenrechts);
            }
            private void OptionClick(object sender, EventArgs e)
            {
                Button s = (Button)sender;

                if (s.Name == "ol")
                    this.value = ElboType.oben_links;
                if(s.Name == "or")
                    this.value = ElboType.oben_rechts;
                if (s.Name == "ul")
                    this.value = ElboType.unten_links;
                if (s.Name == "ur")
                    this.value = ElboType.unten_rechts;

                this.edSvc.CloseDropDown();
            }

            public void End()
            {
                this.edSvc = null;
                this.value = null;
                
            }


            public void Start(IWindowsFormsEditorService edSvc, object value)
            {
                ElboType middleLeft;
                this.edSvc = edSvc;
                this.value = value;
                if (value == null)
                {
                    middleLeft = ElboType.oben_links;
                }
                else
                {
                    middleLeft = (ElboType)value;
                }
                if (middleLeft == ElboType.oben_links)
                    this.obenlinks.BackColor = SystemColors.ActiveCaption;
                if (middleLeft == ElboType.oben_rechts)
                    this.obenrechts.BackColor = SystemColors.ActiveCaption;
                if (middleLeft == ElboType.unten_links)
                    this.untenlinks.BackColor = SystemColors.ActiveCaption;
                if (middleLeft == ElboType.unten_rechts)
                    this.untenrechts.BackColor = SystemColors.ActiveCaption;
            }

            public object Value
            {
                get
                {
                    return value;
                }
            }

            protected override bool ShowFocusCues
            {
                get
                {
                    return true;
                }
            }

        }
    }
}
